package com.zjsru.oneDay202203;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/3/24 图片平滑器
 * <p>
 * 输入:img = [[1,1,1],[1,0,1],[1,1,1]] 输出:[[0, 0, 0],[0, 0, 0], [0, 0, 0]] 解释: 对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) =
 * 平均(0.75) = 0 对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0 对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
 * <p>
 * <p>
 * 输入: img = [[100,200,100],[200,50,200],[100,200,100]] 输出: [[137,141,137],[141,138,141],[137,141,137]] 解释: 对于点 (0,0),
 * (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137 对于点 (0,1), (1,0), (1,2), (2,1):
 * floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141 对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9)
 * = floor(138.888889) = 138
 */
public class imageSmoother {
    
    private static int[][] DIRS = new int[][] {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0},{1, 1}};
    
    public int[][] imageSmoother(int[][] img) {
        int n = img.length, m = img[0].length;
        int[][] ans = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i][j] = healper(img, i, j);
            }
        }
        return ans;
    }
    
    private int healper(int[][] oldImg, int i, int j) {
        int sum = 0, cnt = 0;
        for (int[] dirs : DIRS) {
            int nI = i + dirs[0];
            int nJ = j + dirs[1];
            if (nI >= 0 && nI < oldImg.length && nJ < oldImg[0].length && nJ >= 0) {
                cnt += 1;
                sum += oldImg[nI][nJ];
            }
        }
        return sum/cnt;
    }
    
    
    public static void main(String[] args) {
        imageSmoother imageSmoother = new imageSmoother();
        int[][] img = new int[][] {{100, 200, 100}, {200, 50, 200}, {100, 200, 100}};
        System.out.println(imageSmoother.imageSmoother(img));
    }
}
